//
//  MineDynamicImageView.m
//  WeCommunity
//
//  Created by mac on 2017/6/20.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "MineDynamicImageView.h"

@interface MineDynamicImageView ()

@property (nonatomic,assign) CGFloat imgaeViewWidth;

@end

@implementation MineDynamicImageView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.imageViewArray = [NSMutableArray array];
        
        self.imgaeViewWidth = (CGRectGetWidth(self.bounds) - 4) / 3;
        for (int i = 0; i < 9 ; i++) {
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.imgaeViewWidth + 2) * (i % 3),(self.imgaeViewWidth + 2) * (i / 3), self.imgaeViewWidth, self.imgaeViewWidth)];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
            [self addSubview:imageView];
            
            [self.imageViewArray addObject:imageView];
        }
        
    }
    return self;
}

-(void)setDynamicModel:(DynamicModel *)dynamicModel
{
    _dynamicModel = dynamicModel;
    
    self.imageArray = _dynamicModel.imageList;
}

-(void)setImageArray:(NSArray *)imageArray
{
    _imageArray = imageArray;
    
    //分情况布局
    if (_imageArray.count == 1) {
        UIImageView * imageView = self.imageViewArray[0];
        imageView.frame = self.bounds;
        
    }else if(_imageArray.count == 2){
        CGFloat width = (CGRectGetWidth(self.bounds) - 2) / 2;
        
        UIImageView * imageView1 = self.imageViewArray[0];
        imageView1.frame = CGRectMake(0, 0, width , width);
        UIImageView * imageView2 = self.imageViewArray[1];
        imageView2.frame = CGRectMake(CGRectGetMaxX(imageView1.frame) + 2, 0, width, width);
        
    }else if(_imageArray.count > 2){
        UIImageView * imageView1 = self.imageViewArray[0];
        imageView1.frame = CGRectMake(0, 0, self.imgaeViewWidth , self.imgaeViewWidth);
        UIImageView * imageView2 = self.imageViewArray[1];
        imageView2.frame = CGRectMake(CGRectGetMaxX(imageView1.frame) + 2, 0, self.imgaeViewWidth , self.imgaeViewWidth);
    }
    
    int i = 0;
    for (NSString * imageString in _imageArray) {
        UIImageView * imageView = self.imageViewArray[i];;
        imageView.hidden = NO;
        
        [imageView sd_setImageWithURL:LOADINGTHUMBIMG_URL(imageString,300,300)
                     placeholderImage:[UIImage imageNamed:@"placeholder"]
                              options:SDWebImageRetryFailed
                            completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                
                            }];
        i++;
    }
    for (int j = i; j < 9; j++) {
        UIImageView * imageView = self.imageViewArray[j];
        imageView.hidden = YES;
    }

}



@end
